/******************************************************
*  Copyright(c) pages.ilovezxl.top
*  FileName : util.h
*  Author : rivulet.dave
*  Description : ¹«¹²º¯Êý
*  Data : 2019-04-15
******************************************************/

#ifndef __UTIL_H__
#define __UTIL_H__

/*********************************************************************
 * INCLUDES
 */

#ifdef __cplusplus
extern "C" {
#endif

/*********************************************************************
 *  EXTERNAL VARIABLES
 */

/*********************************************************************
 * TYPES
 */

/*********************************************************************
 * MACROS
 */

#define UTL_BASE64_INVALID_ARG        -1
#define UTL_BASE64_WRONG_FORMAT       -2
#define UTL_BASE64_OUTPUT_OVERFLOW    -3

/*********************************************************************
 * FUNCTIONS
 */

/**
 *   @Description: ×Ö·û´®×ªBCDÂë - Ð¡¶ËÐò£ºÏÈµÍ4Î»£¬ºó¸ß4Î»
 *   @param *puiDest - BCDÂë´æ·ÅµÄÄ¿µÄµØÖ·.
 *   @param *pSrc - ×Ö·û´®µØÖ·.
 *   @return £ºBCDÂëµÄ³¤¶È
 */
uint32_t UTL_Str2BCDLittle(uint8_t *puiDest, const char *pSrc);

/**
 *   @Description: BCDÂë×ª×Ö·û´® - Ð¡¶ËÐò£ºÏÈµÍ4Î»£¬ºó¸ß4Î»
 *   @param *pDest - ×Ö·û´®µØÖ·.
 *   @param *puiSrc - BCDÂë´æ·ÅµÄµØÖ·.
 *   @param uiSrcLen - BCDÂëµÄ³¤¶È.
 *   @return £ºNA
 */
void UTL_BCD2StrLittle(char *pDest, const uint8_t *puiSrc, uint32_t uiSrcLen);

/**
 *   @Description: ×Ö·û´®×ªBCDÂë
 *   @param *puiDest - BCDÂë´æ·ÅµÄÄ¿µÄµØÖ·.
 *   @param *pSrc - ×Ö·û´®µØÖ·.
 *   @return £ºBCDÂëµÄ³¤¶È
 */
uint32_t UTL_Str2BCD(uint8_t *puiDest, const char *pSrc);

/**
 *   @Description: BCDÂë×ª×Ö·û´®
 *   @param *pDest - ×Ö·û´®µØÖ·.
 *   @param *puiSrc - BCDÂë´æ·ÅµÄµØÖ·.
 *   @param uiSrcLen - BCDÂëµÄ³¤¶È.
 *   @return £ºNA
 */
void UTL_BCD2Str(char *pDest, const uint8_t *puiSrc, uint32_t uiSrcLen);

/**
 *   @Description: ×Ö·û´®×ª16½øÖÆ
 *   @param *pDest - 16½øÖÆÊý¾Ý.
 *   @param *puiSrc - ×Ö·û´®µØÖ·.
 *   @return £º16½øÖÆÊý¾Ý³¤¶È
 */
uint32_t UTL_Str2Hex(uint8_t *puiDest, const char *pSrc);

/**
 *   @Description: 16½øÖÆ×ª×Ö·û´®
 *   @param *pDest - ×Ö·û´®µØÖ·.
 *   @param *puiSrc - 16½øÖÆÊý¾Ý.
 *   @param uiSrcLen - 16½øÖÆÊý¾Ý³¤¶È.
 *   @return £ºNA
 */
void UTL_Hex2Str(char *pDest, const uint8_t *puiSrc, uint32_t uiSrcLen);

/**
 *
 */
int32_t UTL_Str2Int(char *pSrc);

/**
 *
 *
 */
char *UTL_Int2Str(char *pDest, int32_t iSrc);

/**
 *   @Description: °´×Ö½Ú¼ÆËãÒì»òÐ£ÑéÂë
 *   @param *pucData - 16½øÖÆÊý¾Ý.
 *   @param *uiLen - ×Ö·û´®µØÖ·.
 *   @return £ºÒì»òÐ£ÑéÂë
 */
uint8_t UTL_CalcByteXORCheckSum(uint8_t *pucData, uint32_t uiLen);

/*************************************************
*  Function:       base64_to_ascii
*  Description:    decodes string from base64 to ascii
*  Input:          const char *in: the input string (NIL-terminated)
*  int inlen: length of input string
*  int maxlen: the output buffer size limit, 0 to ignore
*  Output:         unsigned char *out: decoded output string
*  Return:         length of output string on successful
*  less than 0 on error occur, then the output is invalid
*  Others:
*************************************************/
int32_t UTL_Base64Decode(uint8_t *out, uint8_t *in, int32_t inlen, int32_t maxlen);

/*************************************************
*  Function:       ascii_to_base64
*  Description:    encodes string from ascii to base64
*  Input:          const char *in: the input string (NIL-terminated)
*  int inlen: length of input string
*  int maxlen: the output buffer size limit, 0 to ignore
*  Output:         unsigned char *out: decoded output string
*  Return:         length of output string on successful
*  less than 0 on error occur, then the output is invalid
*  Others:
*************************************************/
int32_t UTL_Base64Encode(int8_t *out, const uint8_t *in, int32_t inlen, int32_t maxlen);

/**
 *   @Description: CRC16-CCITTÐ£Ñé
 *   @param data_arr - »º´æÇø
 *   @param data_len - ´ýÐ£ÑéÊý¾Ý³¤¶È
 *   @return £ºCRC16Ð£ÑéÖµ
 */
uint16_t UTL_Crc16CalcCCITT(uint8_t *data_arr, uint16_t data_len);

void 	 UTL_Crc32TabInit( void ) ;

uint32_t UTL_Crc32Calc(uint32_t u32Crcinit, uint8_t * pBuf ,uint32_t u32BufSize);



void UTL_ToUpper(char *str, uint16_t len);

void UTL_ToLower(char *str, uint16_t len);

/*******************************************************************************
* Function Name      : UTL_SubStr
* Description	     : cut the sub string between string cStart and string cEnd
* Input 		     : cSrc: the string to be cut, cStart: left sub str, cEnd: right sub str
* Output		     : cDest: the target sub str
* Return		     : error code on error, 0 on success.
*******************************************************************************/
int32_t UTL_SubStr(const char * pSrc, const char * pStart, const char * pEnd, char **pDest);

/**
 *
 *
 */
uint16_t UTL_NTOHS(uint16_t usValue);

/**
 *
 *
 */
uint16_t UTL_HTONS(uint16_t usValue);

/**
 *
 *
 */
uint32_t UTL_NTOHL(uint32_t uiValue);

/**
 *
 *
 */
uint32_t UTL_HTONL(uint32_t uiValue);

/**
 *
 *
 */
uint64_t UTL_NTOHLL(uint64_t ulValue);

/**
 *
 *
 */
uint64_t UTL_HTONLL(uint64_t ulValue);

/**
 *
 *
 */
bool UTL_IsLittleEndian(void);

#ifdef __cplusplus
}
#endif /* end of __cplusplus */

#endif
/************************************** The End Of File **************************************/

